﻿@inject IRepository _pizzaStore

		<div class="dialog-title">
			<h2>@Pizza.Special?.Name</h2>
			@Pizza.Special?.Description
		</div>
		<form class="dialog-body">
			<div>
				<label class="@SizeLabelClass" title="@SizeTitle">Size:</label>
				<input type="range" min="@Pizza.MinimumSize" max="@Pizza.MaximumSize" step="1" @bind="Pizza.Size" @bind:event="oninput" />
				<span class="size-label">
					@(Pizza.Size)" (£@(Pizza.GetFormattedTotalPrice()))
				</span>
			</div>
			<div>
				<label>Extra Toppings:</label>
				@if (toppings is null)
				{
					<select class="custom-select" disabled>
						<option>(loading...)</option>
					</select>
				}
				else if (Pizza.Toppings.Count >= 6)
				{
					<div>(maximum reached)</div>
				}
				else
				{
					<select class="custom-select" @onchange="ToppingSelected">
						<option value="-1" disabled selected>(select)</option>
						@for (var i = 0; i < toppings.Count; i++)
						{
							<option value="@i">@toppings[i].Name - (£@(toppings[i].GetFormattedPrice()))</option>
						}
					</select>
				}
			</div>

			<div class="toppings">
				@foreach (var topping in Pizza.Toppings)
				{
					if (topping?.Topping is not null)
					{
						<div class="topping">
							@topping.Topping.Name
							<span class="topping-price">@topping.Topping.GetFormattedPrice()</span>
							<button type="button" class="delete-topping" @onclick="@(() => RemoveTopping(topping.Topping))">x</button>
						</div>
					}
				}
			</div>
		</form>
		<div class="dialog-buttons">
			<button class="btn btn-secondary mr-auto" @onclick="OnCancel">Cancel</button>
			<span class="mr-center">
				Price: <span class="price">@(Pizza.GetFormattedTotalPrice())</span>
			</span>
			<button class="btn btn-success ml-auto" @onclick="OnConfirm">Order ></button>
		</div>


@code {
	[Parameter, EditorRequired] public Pizza Pizza { get; set; } = new();

	[Parameter, EditorRequired] public EventCallback OnCancel { get; set; }
	[Parameter, EditorRequired] public EventCallback OnConfirm { get; set; }

	List<Topping> toppings = null!;

	private const int SmallPizzaSize = 9;
	private const int LargePizzaSize = 14;

	string SizeLabelClass => Pizza.Size switch
	{
		<= SmallPizzaSize => "pizza-small",
		>= LargePizzaSize => "pizza-large",
		_ => "pizza-medium"
	};

	string SizeTitle => Pizza.Size switch
	{
		<= SmallPizzaSize => "That's a small pizza, you've got a family to feed.... why not order more?",
		>= LargePizzaSize => "Now that's what I'm talking about!  Why not try an American Sized JUMBO pizza?",
		_ => "That's a medium pizza, there's more to be ordered if you'd like it"
	};

	protected async override Task OnInitializedAsync()
	{
		toppings = await _pizzaStore.GetToppings();
	}

	void ToppingSelected(ChangeEventArgs e)
	{
		if (int.TryParse((string?)e.Value, out var index) && index >= 0)
		{
			AddTopping(toppings[index]);
		}
	}

	void AddTopping(Topping topping)
	{
		if (toppings is null) return;
		if (Pizza.Toppings.Find(pt => pt.Topping == topping) is null)
		{
			Pizza.Toppings.Add(new PizzaTopping() { Topping = topping });
		}
	}

	void RemoveTopping(Topping topping)
	{
		Pizza.Toppings.RemoveAll(pt => pt.Topping == topping);
	}
}